package com.nova.bluetooth.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;


public class CommonDialog {
    public CommonDialog(Context context, View v) {
        this.mContext = context;
        this.view = v;
        dialog = new Dialog(mContext, 0);//可以在style中设定dialog的样式
        initDialog();
    }

    public void setCancelable( boolean cancelable){
        dialog.setCancelable(cancelable);
    }
    public void setContentView(View view) {
        if (dialog != null) {
            dialog.setContentView(view);
        }
    }

    public void setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
        dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
    }

    public CommonDialog(Activity context, int viewId) {
        this.mContext = context;
        this.viewId = viewId;
        dialog = new Dialog(mContext);//可以在style中设定dialog的样式
        initDialog();
    }

    private boolean isNullBg = false;

    public boolean isShowing() {
        return dialog.isShowing();
    }

    public CommonDialog() {

    }

    public Dialog getDialog() {
        return dialog;
    }

    public void setBgNull(boolean isBgNull) {
        isNullBg = isBgNull;
    }


    public WindowManager.LayoutParams getLayoutParams() {
        return dialog.getWindow().getAttributes();
    }


    public void setLayoutParams(WindowManager.LayoutParams layoutParams) {
        dialog.getWindow().setAttributes(layoutParams);
    }

    public void setSystemAlertType(boolean isSystemAlert) {
        if (isSystemAlert) {
            dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
        }
    }

    @SuppressLint("WrongConstant")
    private void initDialog() {
        if (mOnDismissListener != null) {
            dialog.setOnDismissListener(mOnDismissListener);
        }

        if (onCancelListener != null) {
            dialog.setOnCancelListener(onCancelListener);
        }

        if (view == null) {
            view = View.inflate(mContext, viewId, null);
        }

        dialog.setContentView(view);
//        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
//        lp.width = getResources().getDimensionPixelOffset(R.dimen.y350); //设置宽度

        WindowManager.LayoutParams lp = dialog.getWindow().getAttributes();

        if (isNullBg) {
            dialog.getWindow().setDimAmount(0f);
        }

        if (this.width == 0) {
            lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
        } else if (this.width == -1) {
            lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        } else {
            lp.width = mContext.getResources().getDimensionPixelOffset(this.width); //设置宽度
        }
        if (this.height == 0) {
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        } else if (this.height == -1) {
            lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        } else {
            lp.height = mContext.getResources().getDimensionPixelOffset(this.height);
        }
        if (this.gravity == 10000) {
            lp.gravity = Gravity.BOTTOM;
        } else {
            lp.gravity = gravity;
        }


        dialog.getWindow().setAttributes(lp);
        //设置该属性，dialog可以铺满屏幕
        dialog.getWindow().setBackgroundDrawable(null);
    }

    private DialogInterface.OnCancelListener onCancelListener;

    public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }

    public void setOnDismissListener(DialogInterface.OnDismissListener onDismissListener) {
        this.mOnDismissListener = onDismissListener;
    }

    private DialogInterface.OnDismissListener mOnDismissListener;

    private Dialog dialog;
    private Context mContext;

    public void setWidth(int widthDimenId) {
        this.width = widthDimenId;
        initDialog();
    }

    public void setHeight(int heightDimenId) {
        this.height = heightDimenId;
        initDialog();
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
        initDialog();
    }

    private int width = 0;
    private int height = 0;
    private View view;
    private int viewId = 0;
    private int gravity = 10000;

    public void showDialog() {
        try {
            if (!isShowing()) {
                dialog.show();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void dismissDialog() {
        try {
            if (dialog != null) {
                if (isShowing()) {
                    dialog.dismiss();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    };
}
